package com.kfm.service;

import com.kfm.dao.StudentDao;
import com.kfm.model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentService {
    StudentDao dao;
    SqlSession sqlSession;
    InputStream inputStream;
    public StudentService(){
        try {
            inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = builder.build(inputStream);
        sqlSession = build.openSession(true);
        dao = sqlSession.getMapper(StudentDao.class);
    }
    /*
    查询学生信息
     */
    public List<Student> queryStudent(){
        return dao.selectAll();
    }

    /*
    * 添加学生信息
     */
    public int addStudent(Student student){
        return dao.insertStudent(student);
    }
    /*
    * 修改学生信息
     */
    public int updateStudent(Student student){
        return dao.updateStudent(student);
    }
    /*
    * 删除学生信息
     */
    public int deleteStudent(int id){
        return dao.deleteStudent(id);
    }

    /*
    分页查询学生信息
     */
    public List<Student> queryStudentByPage(int page,int pageSize){
        return dao.queryStudentsWithPaging(page,pageSize);
    }


}
